/**
 * PLM项目模块解析助手
 * 用于解决模块路径导入问题
 */

// 缓存已解析的模块
const resolvedModules = {};

// 模块映射表 - 将错误的导入路径映射到正确的模块
const MODULE_MAP = {
  // 常见的路径错误修复
  '../../../../helpers/log_helper.js': '../../../helpers/log_helper.js',
  '../../../../../helpers/log_helper.js': '../../../helpers/log_helper.js',
  '../../../../projects/helpers/log_helper.js': '../../../helpers/log_helper.js',
};

/**
 * 尝试解析模块 - 静态版本
 * @param {String} modulePath 模块路径
 * @return {Object|null} 解析的模块或null
 */
function tryResolveModule(modulePath) {
  // 如果已经缓存，直接返回
  if (resolvedModules[modulePath]) {
    return resolvedModules[modulePath];
  }
  
  // 根据不同的路径分别处理
  let module = null;
  
  // 处理映射路径
  if (modulePath === '../../../../helpers/log_helper.js') {
    try {
      module = require('../../../helpers/log_helper.js');
      console.log(`[模块解析] 成功将 ${modulePath} 重定向到 ../../../helpers/log_helper.js`);
    } catch (e) {
      console.error(`[模块解析] 重定向模块导入失败: ${modulePath} -> ../../../helpers/log_helper.js`, e);
      module = createMockLogHelper();
    }
  } 
  else if (modulePath === '../../../../../helpers/log_helper.js') {
    try {
      module = require('../../../helpers/log_helper.js');
      console.log(`[模块解析] 成功将 ${modulePath} 重定向到 ../../../helpers/log_helper.js`);
    } catch (e) {
      console.error(`[模块解析] 重定向模块导入失败: ${modulePath} -> ../../../helpers/log_helper.js`, e);
      module = createMockLogHelper();
    }
  }
  else if (modulePath === '../../../../projects/helpers/log_helper.js') {
    try {
      module = require('../../../helpers/log_helper.js');
      console.log(`[模块解析] 成功将 ${modulePath} 重定向到 ../../../helpers/log_helper.js`);
    } catch (e) {
      console.error(`[模块解析] 重定向模块导入失败: ${modulePath} -> ../../../helpers/log_helper.js`, e);
      module = createMockLogHelper();
    }
  }
  // 静态处理其他可能的模块路径
  else if (modulePath === '../../../helpers/page_helper.js') {
    try {
      module = require('../../../helpers/page_helper.js');
    } catch (e) {
      console.error(`[模块解析] 导入失败: ${modulePath}`, e);
      module = createMockPageHelper();
    }
  }
  else if (modulePath === '../../../helpers/cloud_helper.js') {
    try {
      module = require('../../../helpers/cloud_helper.js');
    } catch (e) {
      console.error(`[模块解析] 导入失败: ${modulePath}`, e);
      module = createMockCloudHelper();
    }
  }
  else if (modulePath === '../../../helpers/time_helper.js') {
    try {
      module = require('../../../helpers/time_helper.js');
    } catch (e) {
      console.error(`[模块解析] 导入失败: ${modulePath}`, e);
      module = createMockTimeHelper();
    }
  }
  else if (modulePath === '../../../config/routes_config.js') {
    try {
      module = require('../../../config/routes_config.js');
    } catch (e) {
      console.error(`[模块解析] 导入失败: ${modulePath}`, e);
      module = createMockRoutesConfig();
    }
  }
  else if (modulePath === '../services/research_service.js') {
    try {
      module = require('../services/research_service.js');
    } catch (e) {
      console.error(`[模块解析] 导入失败: ${modulePath}`, e);
      module = createMockResearchService();
    }
  }
  else if (modulePath === 'mobx-miniprogram') {
    try {
      module = require('mobx-miniprogram');
    } catch (e) {
      console.error(`[模块解析] 导入失败: ${modulePath}`, e);
      module = createMockMobx();
    }
  }
  else {
    // 无法静态解析的模块，提供默认的模拟实现
    console.error(`[模块解析] 无法静态解析模块路径: ${modulePath}`);
    module = createMockModule(modulePath);
  }
  
  // 缓存结果
  resolvedModules[modulePath] = module;
  return module;
}

/**
 * 创建模拟日志助手
 * @return {Object} 模拟的日志模块对象
 */
function createMockLogHelper() {
  return {
    logEvent: function(event, data) {
      console.log(`[Mock日志] ${event}`, data);
    },
    logError: function(error, context) {
      console.error(`[Mock日志] 错误:`, error, context);
    },
    logPageView: function(page) {
      console.log(`[Mock日志] 页面浏览: ${page}`);
    }
  };
}

/**
 * 创建模拟页面助手
 * @return {Object} 模拟的页面助手对象
 */
function createMockPageHelper() {
  return {
    showModal: function(title, content) {
      console.log(`[Mock页面助手] 显示模态框: ${title} - ${content}`);
    },
    showErrToast: function(err) {
      console.error(`[Mock页面助手] 错误提示:`, err);
    },
    showLoading: function(title) {
      console.log(`[Mock页面助手] 显示加载: ${title}`);
    },
    hideLoading: function() {
      console.log(`[Mock页面助手] 隐藏加载`);
    }
  };
}

/**
 * 创建模拟云助手
 * @return {Object} 模拟的云助手对象
 */
function createMockCloudHelper() {
  return {
    callCloudData: async function(route, data, options) {
      console.log(`[Mock云助手] 调用云函数: ${route}`, data, options);
      return { success: true, data: {} };
    },
    callCloud: async function(route, data, options) {
      console.log(`[Mock云助手] 调用云函数: ${route}`, data, options);
      return { success: true, data: {} };
    }
  };
}

/**
 * 创建模拟时间助手
 * @return {Object} 模拟的时间助手对象
 */
function createMockTimeHelper() {
  return {
    formatDate: function(date) {
      return date instanceof Date ? date.toISOString() : new Date().toISOString();
    },
    getNow: function() {
      return new Date().toISOString();
    },
    getTimeID: function() {
      return Date.now().toString();
    }
  };
}

/**
 * 创建模拟路由配置
 * @return {Object} 模拟的路由配置对象
 */
function createMockRoutesConfig() {
  return {
    routes: {
      index: '/projects/plm/pages/default/index/default_index',
      project: {
        list: '/projects/plm/pages/project/list/project_list',
        detail: '/projects/plm/pages/project/detail/project_detail',
        edit: '/projects/plm/pages/project/edit/project_edit'
      }
    }
  };
}

/**
 * 创建模拟研发服务
 * @return {Object} 模拟的研发服务对象
 */
function createMockResearchService() {
  return {
    researchService: {
      getResearchDetail: async function(id) {
        console.log(`[Mock研发服务] 获取研发详情: ${id}`);
        return { _id: id, name: '模拟研发项目', status: 0 };
      },
      executeEvent: async function(id, eventId, data) {
        console.log(`[Mock研发服务] 执行事件: ${id} - ${eventId}`, data);
        return { success: true, project: { _id: id, name: '模拟研发项目', status: 1 } };
      }
    }
  };
}

/**
 * 创建模拟Mobx
 * @return {Object} 模拟的Mobx对象
 */
function createMockMobx() {
  return {
    observable: function(obj) { return obj; },
    action: function(fn) { return fn; },
    computed: function(fn) { return fn; },
    runInAction: function(fn) { return fn(); }
  };
}

/**
 * 创建模拟模块
 * @param {String} modulePath 模块路径
 * @return {Object} 模拟的模块对象
 */
function createMockModule(modulePath) {
  // 根据模块路径创建不同的模拟模块
  if (modulePath.includes('log_helper')) {
    return createMockLogHelper();
  }
  if (modulePath.includes('page_helper')) {
    return createMockPageHelper();
  }
  if (modulePath.includes('cloud_helper')) {
    return createMockCloudHelper();
  }
  if (modulePath.includes('time_helper')) {
    return createMockTimeHelper();
  }
  
  // 默认通用模拟模块
  return {
    __isMockModule: true,
    __originalPath: modulePath,
    default: function() { 
      console.warn(`[模拟模块] 调用了模拟模块: ${modulePath}`);
    }
  };
}

// 导出模块解析函数
module.exports = {
  resolve: tryResolveModule,
  resolvedModules,
  MODULE_MAP
}; 